package com.mall.page.web.controller;

import com.mall.common.util.RespResult;
import com.mall.page.web.service.ISeckillPageService;
import com.mall.seckill.feign.SeckillGoodsFeign;
import com.mall.seckill.model.SeckillGoods;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * @author Darren
 * @date 2022-09-28 16:03
 * Description：
 */
@RestController
@RequestMapping("/page")
public class SeckillPageController {
    @Autowired
    private ISeckillPageService seckillPageService;
    @Autowired
    private SeckillGoodsFeign seckillGoodsFeign;

    @GetMapping("/seckill/goods/{id}")
    @ApiOperation("生成秒杀商品详情静态页")
    public RespResult page(@PathVariable("id") String id) throws FileNotFoundException, UnsupportedEncodingException {
        seckillPageService.generateHtml(id);
        return RespResult.ok();
    }

    @DeleteMapping("/seckill/goods/{actId}")
    @ApiOperation("删除秒杀商品详情静态页")
    public RespResult deleteByActId(@PathVariable("actId") String actId) {
        // 查询当前活动id对应的商品列表
        RespResult<List<SeckillGoods>> respResult = seckillGoodsFeign.getSeckillGoodsByActId(actId);
        List<SeckillGoods> goodsList = respResult.getData();
        // 删除所有页面
        if (goodsList != null) {
            for (SeckillGoods seckillGoods : goodsList) {
                seckillPageService.deleteHtml(seckillGoods.getId());
            }
        }
        return RespResult.ok();
    }
}
